home *** CD-ROM | disk | FTP | other *** search
/ Star Trek Starfleet Academy Mini Omnipedia / MINI_OMNI.ISO / pc / omni_v2.dxr / 00414_Speech Recognition.ls < prev    next >
Encoding:
Text File  |  1996-04-15  |  9.0 KB  |  351 lines

  1. global oASRObj, gMainLangModel, gCallBack, gSelectorLangModel, gTempLangModel, gMainLexSize, gMainLex, gMACListenStatus, gTempLexSize, gTempLexicon, gInputField, gdescfield, gIndxScrollFldK, gComputerTypeK, gVideoButtspr, gSelectorLex, gSelectorLexSize, gHuhCount, gESstartSND, oTextScroller, oIndexScroller, oINTERfaceFind, gPrevCommand, gBtnHitSND, gQTflag, gModeState, gTrekCursor, gSpeeButtSpr, gSpeeButtCastList, gWINListenStatus, gWinVoiceEnabled, gAudioButtspr, gWINvoiUserPermit, gSpeechCapable, gTempHyperlinks, gHoldSearchInfo, gResrcButtspr, oTextSlider, gImInHelp, gSpeechWasChosenF, gWinVoiceXobj, gTickSND, gFirstSpeechF
  2.  
  3. on hInitRecongition
  4.   hputmsg(">hInitRecongition begins==================================<")
  5.   put "Determining voice recognition capability..." into field "Status"
  6.   cursor(4)
  7.   set gSpeechCapable to 0
  8.   set gMainLangModel to 0
  9.   set gTempLangModel to 0
  10.   set gSelectorLangModel to 0
  11.   set gTempLexicon to []
  12.   set gMACListenStatus to 0
  13.   set gPrevCommand to EMPTY
  14.   if gComputerTypeK = #mac then
  15.     if factory("ASRXObject") = 0 then
  16.       set Lfilepath to the pathName & "ASR.XObj"
  17.       if not hFileIsHere(Lfilepath) then
  18.         hReportStatus("The file " & QUOTE & "ASR.XObj" & QUOTE & " must be present.")
  19.         hearit(gNopeSND)
  20.         cursor([gTrekCursor, gTrekCursor + 1])
  21.         return 0
  22.       end if
  23.       openXLib(Lfilepath)
  24.     end if
  25.     set result to hStartASR()
  26.     if result < 0 then
  27.       hReportStatus("Speech recognition not enabled.")
  28.       hearit(gNopeSND)
  29.     else
  30.       if gFirstSpeechF then
  31.         go("OpC3")
  32.       end if
  33.       hearit(gBtnHitSND)
  34.       hReportStatus("Loading primary command list...")
  35.       hMakeMainCommandList()
  36.       hReportStatus("Loading secondary command list...")
  37.       hMakeSelectorList()
  38.       set gSpeechCapable to 1
  39.       put "Speech recognition sucessfully enabled." into field "Status"
  40.       hUploadTempLinksLang()
  41.       hUploadSelectorLang()
  42.       hearit(gTickSND)
  43.       hUploadMainLanguage()
  44.       hearit(gTickSND)
  45.       hSetLanguage(gMainLangModel)
  46.     end if
  47.   end if
  48.   cursor([gTrekCursor, gTrekCursor + 1])
  49. end
  50.  
  51. on hStartASR
  52.   if objectp(oASRObj) then
  53.     oASRObj(mdispose)
  54.   end if
  55.   set oASRObj to ASRXObject(mnew, factory("ASRCallBack"))
  56.   set result to value(oASRObj)
  57.   return result
  58. end
  59.  
  60. on hRequestSpeechStatus
  61.   if gComputerTypeK = #mac then
  62.     if gMACListenStatus then
  63.       if oASRObj(mStatus) > 1 then
  64.         hShowSpeechStatus(#STANDBY)
  65.       else
  66.         hShowSpeechStatus(#READY)
  67.       end if
  68.     end if
  69.   else
  70.     if gComputerTypeK = #WIN then
  71.       hWinVoiceIdle()
  72.     end if
  73.   end if
  74. end
  75.  
  76. on hInitSpeeButts
  77.   puppetSprite(gSpeeButtSpr, 1)
  78.   set gSpeeButtCastList to [:]
  79.   setaProp(gSpeeButtCastList, #INACTIVE, the number of cast "SPEECHavail")
  80.   setaProp(gSpeeButtCastList, #STANDBY, the number of cast "SPEECHstandby")
  81.   setaProp(gSpeeButtCastList, #READY, the number of cast "SPEECHready")
  82.   setaProp(gSpeeButtCastList, #null, the number of cast "PixelREF")
  83. end
  84.  
  85. on hShowSpeechStatus pMsg
  86.   if pMsg = #STANDBY then
  87.     hSysAlert(#null, 1)
  88.   end if
  89.   if pMsg = #READY then
  90.     hSysAlert(#LCARSREADY, 1)
  91.   end if
  92. end
  93.  
  94. on hFlashSpeechStatus
  95. end
  96.  
  97. on hStartListening pNoAnnc
  98.   if gComputerTypeK = #WIN then
  99.     hWinVoiceMicOn(0)
  100.   else
  101.     if objectp(oASRObj) then
  102.       hReportStatus("Preparing speech recognizers...")
  103.       set gMACListenStatus to 1
  104.       oASRObj(mStartListening)
  105.       hReportStatus("Speech ready...")
  106.       if voidp(pNoAnnc) then
  107.         hAnnounce("Voice Rec Activated")
  108.       end if
  109.     end if
  110.   end if
  111. end
  112.  
  113. on hUploadMainLanguage
  114.   if gMainLangModel = 0 then
  115.     set gMainLangModel to oASRObj(mNewLanguage, "<Main>")
  116.     hPrepALanguage(gMainLangModel, gMainLexSize, gMainLex)
  117.   end if
  118. end
  119.  
  120. on hSafeRelease pLM
  121.   if pLM then
  122.     oASRObj(mRelease, pLM)
  123.   end if
  124.   return 0
  125. end
  126.  
  127. on hSetLanguage pLM
  128.   if pLM then
  129.     oASRObj(mSetLanguage, pLM)
  130.   end if
  131. end
  132.  
  133. on hUploadSelectorLang
  134.   if gSelectorLangModel = 0 then
  135.     set gSelectorLangModel to oASRObj(mNewLanguage, "<Selectors>")
  136.     hPrepALanguage(gSelectorLangModel, gSelectorLexSize, gSelectorLex)
  137.   end if
  138. end
  139.  
  140. on hPrepALanguage pLM, pSize, pList
  141.   cursor(4)
  142.   set Lerrmsg to 0
  143.   repeat with i = 1 to pSize
  144.     set Lerrmsg to oASRObj(mAddPhrase, pLM, getPropAt(pList, i), i)
  145.     if Lerrmsg then
  146.       exit repeat
  147.     end if
  148.   end repeat
  149.   cursor([gTrekCursor, gTrekCursor + 1])
  150.   if Lerrmsg then
  151.     hReportStatus("Speech recognizer had trouble adding the phrase >" & getPropAt(pList, i))
  152.     hearit(gNopeSND)
  153.   end if
  154. end
  155.  
  156. on hRestoreSpeech
  157.   if gSpeechCapable = 0 then
  158.     return 
  159.   end if
  160.   if gSpeechWasChosenF = 1 then
  161.     if gComputerTypeK = #mac then
  162.       if gMACListenStatus = 0 then
  163.         hStartListening(1)
  164.       end if
  165.     else
  166.       if gComputerTypeK = #WIN then
  167.         if hWinVoiceMicState() = 0 then
  168.           hStartListening(1)
  169.         end if
  170.       end if
  171.     end if
  172.   end if
  173. end
  174.  
  175. on hSwitchSpeech
  176.   if gSpeechCapable = 0 then
  177.     return 
  178.   end if
  179.   if gComputerTypeK = #mac then
  180.     if gMACListenStatus = 0 then
  181.       set gSpeechWasChosenF to 1
  182.       if not gFirstSpeechF then
  183.         hInitRecongition()
  184.         set gFirstSpeechF to 0
  185.       end if
  186.       hStartListening()
  187.     else
  188.       set gSpeechWasChosenF to 0
  189.       set gFirstSpeechF to 0
  190.       hStopListening()
  191.     end if
  192.   else
  193.     if gComputerTypeK = #WIN then
  194.       if not objectp(gWinVoiceXobj) then
  195.         set gSpeechWasChosenF to 1
  196.         hWinVoiceOpen(0)
  197.         if gWinVoiceEnabled = 1 then
  198.           put "ANNOUNCING FROM HSWITCHSPEECH"
  199.           hAnnounce("Voice Rec Activated")
  200.         end if
  201.       else
  202.         if hWinVoiceMicState() = 0 then
  203.           set gSpeechWasChosenF to 1
  204.           hStartListening()
  205.         else
  206.           set gSpeechWasChosenF to 0
  207.           hWinVoiceMicOff()
  208.         end if
  209.       end if
  210.     end if
  211.   end if
  212. end
  213.  
  214. on mDoRecognize phrase, phraseID
  215.   global gMainLex
  216.   hearit(gESstartSND)
  217.   hReportStatus("Interpreting >" & phrase & "<")
  218.   set Ltidl to the itemDelimiter
  219.   set the itemDelimiter to ","
  220.   set LthisPhr to value(item 1 of phraseID)
  221.   if LthisPhr < 1 then
  222.     put " which I dont understand." after field "Status"
  223.     set gHuhCount to gHuhCount + 1
  224.     if gHuhCount > 5 then
  225.       if gHuhCount > 10 then
  226.         hAnnounce("Please Specify")
  227.         set gHuhCount to 0
  228.       else
  229.         if gHuhCount = 6 then
  230.           hAnnounce("...Do not address...In that...")
  231.         end if
  232.       end if
  233.     end if
  234.     set the itemDelimiter to Ltidl
  235.     return 
  236.   else
  237.     set gHuhCount to 0
  238.   end if
  239.   hSysAlert(#null)
  240.   if LthisPhr <= gMainLexSize then
  241.     if LthisPhr > 2 then
  242.       set the itemDelimiter to Ltidl
  243.       do(string(getAt(gMainLex, LthisPhr)))
  244.       hRememberThis(getAt(gMainLex, LthisPhr))
  245.     else
  246.       set LtheSubID to value(item 2 of phraseID)
  247.       set the itemDelimiter to Ltidl
  248.       if LthisPhr = 1 then
  249.         do(string(getAt(gSelectorLex, LtheSubID)))
  250.         hRememberThis(getAt(gSelectorLex, LtheSubID))
  251.       else
  252.         if LthisPhr = 2 then
  253.           set tidl to the itemDelimiter
  254.           set the itemDelimiter to "@"
  255.           hInterpretSpecial(item LtheSubID of gTempHyperlinks, #ENCY)
  256.           set the itemDelimiter to tidl
  257.         else
  258.         end if
  259.       end if
  260.     end if
  261.     hSysAlert(#LCARSREADY)
  262.   else
  263.     hReportStatus("==Yeah? you want me to " & phrase & "!")
  264.   end if
  265. end
  266.  
  267. on hStopSpeech
  268.   hStopListening()
  269.   if gComputerTypeK = #WIN then
  270.     hWinVoiceClose()
  271.   else
  272.     if objectp(oASRObj) then
  273.       set gMainLangModel to hSafeRelease(gMainLangModel)
  274.       set gTempLangModel to hSafeRelease(gTempLangModel)
  275.       set gSelectorLangModel to hSafeRelease(gSelectorLangModel)
  276.       oASRObj(mdispose)
  277.     end if
  278.   end if
  279. end
  280.  
  281. on hStopListening
  282.   if gComputerTypeK = #WIN then
  283.     hWinVoiceMicOff()
  284.     hShowSpeechStatus(#INACTIVE)
  285.   else
  286.     if objectp(oASRObj) then
  287.       if not voidp(gSpeechWasChosenF) then
  288.         hReportStatus("Speech recognition turned off...")
  289.       end if
  290.       oASRObj(mStopListening)
  291.       set gMACListenStatus to 0
  292.       hShowSpeechStatus(#INACTIVE)
  293.     end if
  294.   end if
  295. end
  296.  
  297. on hMakeMainCommandList
  298.   set gMainLex to [:]
  299.   do(the text of cast "Lex Phrases - Main")
  300.   set gMainLexSize to count(gMainLex)
  301. end
  302.  
  303. on hMakeSelectorList
  304.   set gSelectorLex to [:]
  305.   sort(gSelectorLex)
  306.   do(the text of cast "Lex Phrases - Selectors")
  307.   set gSelectorLexSize to count(gSelectorLex)
  308. end
  309.  
  310. on hReply pWhatStr
  311.   if pWhatStr = #Dear then
  312.     hAnnounce("You're Welcome, Darling")
  313.   else
  314.     if pWhatStr = #Pardon then
  315.       hAnnounce("Not a replicator")
  316.     else
  317.       if pWhatStr = #HELLO then
  318.         hReportStatus("Greetings to you.")
  319.       else
  320.         hAnnounce("You're welcome")
  321.         hReportStatus(pWhatStr)
  322.       end if
  323.     end if
  324.   end if
  325. end
  326.  
  327. on hDoThatAgain
  328.   do(string(gPrevCommand))
  329. end
  330.  
  331. on hRememberThis pThis
  332.   if not pThis contains "Do That Again" then
  333.     set gPrevCommand to pThis
  334.   end if
  335. end
  336.  
  337. on hInterpretSpecial pChunk, pMode
  338.   if pChunk = EMPTY then
  339.     abort()
  340.   end if
  341.   set Ltarget to hcleanup(pChunk)
  342.   put Ltarget into field gInputField
  343.   hilite char 999 of field gInputField
  344.   hDetectMode(Ltarget)
  345.   hMiniFind()
  346. end
  347.  
  348. factory ASRCallBack
  349. method mRecognize phrase, id
  350.   mDoRecognize(phrase, id)
  351.